#!/usr/bin/perl -w
##############################################################################
#
# test_IperfServer_Obj.pl
# =======================
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   This script demonstrates how to use IperfServer.pm and IperfClient.pm
#   classes. 
#
# Steps:
#
#   1.) Instantiate an IperfServer object and call it Server1
#   2.) Start a telnet session to target host
#   3.) Log the entire telnet session to a file
#   4.) Login to the target host
#   5.) Change the MTU size of the hosting Linux OS
#   6.) Start iperf in server mode
#   7.) Repeat step 1 to 6 but for IperfClient class
#   8.) Stop iperf server after client is done testing
#   9.) Output test result to screen
#  10.) Exit
#
# Expected result:
#
#   Successful run with iperf test result output
#
##############################################################################

package main;
$VERSION = 1.01;

# If the PERLLIB environment variable is not set like
# "export PERLLIB=<lib directory>" then uncomment the
# following two lines:
# use FindBin qw($Bin); 
# use lib "$Bin/./lib";
use Node;
use Linux;
use IperfServer;

my $Server1 = undef;
use vars qw($iperfServerIP);

if ((defined $ENV{'PAT_ROOT'}) && (defined $ENV{'PAT_LOG'})) {

  # variables init:
  $self->{iperfServerIP} = "118.1.1.1";
  $self->{iperfClientIP} = "119.1.1.1";
  $self->{resultFileName} = "iperfServerTestResult.txt";

  # Instantiate a Linux based IperfServer object and call it Server1
  $Server1 = IperfServer->new("localhost", "tester", "password", 23);

  # Instantiate a Linux based IperfClient object and call it Client1
  $Client1 = IperfServer->new("localhost", "tester", "password", 23);

  # Start a telnet session to target host
  $Server1->openTelnet();

  # Start a telnet session to target host
  $Client1->openTelnet();

  # Log the entire telnet session to a file
  $Server1->log($ENV{'PAT_LOG'} . "test_IperfServer_Obj.log");

  # Log the entire telnet session to a file
  $Client1->log($ENV{'PAT_LOG'} . "test_IperfClient_Obj.log");

  # Login to the target host
  $Server1->login(qr/login/, qr/Password:/, qr/autons tester/);

  # Login to the target host
  $Client1->login(qr/login/, qr/Password:/, qr/autons tester/);

  # Execute Linux's "su" command
  $Server1->su("password", qr/Password:/, qr/#/);

  # Execute Linux's "su" command
  $Client1->su("password", qr/Password:/, qr/#/);

  # Kill the Iperf process
  $Server1->kill("iperf");

  # Set MTU size of a network port
  $Server1->setMTU($self->{iperfServerIP}, "512");

  # Set MTU size of a network port
  $Client1->setMTU($self->{iperfClientIP}, "512");

  # Start iperf server in UDP mode
  $Server1->start($self->{iperfServerIP}, $self->{resultFileName}, "-u");

  # Start iperf server in UDP mode
  $Client1->start($self->{iperfServerIP}, 30, "2M", "-u", "512");

  # Stop iperf server and output result to console
  my $testResult = $Server1->getResult($self->{resultFileName});
  print "\n============= Iperf Server Test Result ================";
  print "\n $testResult ";
  print "\n=======================================================\n";

  # Release Server1's resource
  $Server1->DESTROY();
}

undef $Server1;
undef $Client1;
